<?php
namespace hid\storages\apcu;

use hid\base\IdBuilder;
use Exception;
use hid\base\LocalStorage;

/**
 * apac内存id存储器
 *<B>说明：</B>
 *<pre>
 * 略
 *</pre>
 */
class ApcuStorage extends LocalStorage
{
    public function lock(string $name,int $tll = 0):bool
    {
        return apcu_add($name,1,$tll);
    }

    public function exists(string $name):bool
    {
        return apcu_exists($name);
    }

    public function unlock(string $name):bool
    {
        return apcu_delete($name);
    }

    public function setValue(string $name,$value):bool
    {
        return apcu_store($name,$value);
    }

    public function getValue(string $name)
    {
        return apcu_fetch($name);
    }

    public function incrBy(IdBuilder $idBuilder,int $step = 1):?int
    {
        $key = $idBuilder->getStoreKey();
        if ($idBuilder->isInitValue() && !$this->hasInitValue($key) && !apcu_exists($key)) {
            apcu_add($key,$idBuilder->getInitValueForStorage());
            $this->addInitValueCache($key);
        }

        return apcu_inc($key,$step);
    }
}
